#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by master on 2017/5/5

from bs4 import BeautifulSoup
import re

from SougouWeixin import SpiderDownloader
from SougouWeixin.Article import Article


def parser(html):
    soup = BeautifulSoup(html, "lxml")

    list_news = soup.find('div', class_='news-box').find('ul', class_='news-list').find_all('li')

    # print(len(list_news))
    list_article = []
    if list_news is not None:
        for news in list_news:
            soup_news = BeautifulSoup(str(news), "lxml")
            article_title = soup_news.find(uigs=re.compile('article_title')).text
            article = Article(article_title)
            article_info = soup_news.find('div', class_='txt-box').find('p').text
            article_link = soup_news.find(uigs=re.compile('article_title'))['href']
            article_source = soup_news.find('div', class_='s-p').find('a').text
            article_source_link = soup_news.find('div', class_='s-p').find('a')['href']
            post_date = soup_news.find('div', class_='s-p').find('span', class_='s2').text
            article_post_date = re.search("\d+", post_date).group()
            image_box = soup_news.find('div', class_='txt-box').find('div', class_='img-d')
            list_images = []
            if image_box is not None:
                for image in image_box.find_all('a'):
                    list_images.append(image.find('span').find('img')['src'])

            article.title = article_title
            article.info = article_info
            article.link = article_link
            article.source = article_source
            article.source_link = article_source_link
            article.post_date = article_post_date
            article.images = list_images
            list_article.append(article)

    return list_article


if __name__ == '__main__':
    for i in range(1, 10):
        html = SpiderDownloader.download("银联", i)
        list_articles = parser(html)
        for a in list_articles:
            a.__str__()
